<?php
/**
 * @package Admin
 * @author Henrique Droog <henridroog@gmail.com>
 * @name SubmenuMapper
 * @since 02/04/2011
 * @version 1.0
 */
class Admin_Model_SubmenuMapper {

    protected $_dbTable;

    public function setDbTable($dbTable) {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable() {
        if (null === $this->_dbTable) {
            $this->setDbTable('Admin_Model_DbTable_Submenu');
        }
        return $this->_dbTable;
    }

    public function save(Admin_Model_Submenu $submenu) {
        $data = array(
            'submenu_name' => $submenu->getName(),
            'submenu_status' => $submenu->getStatus(),
            'menu_id' => $submenu->getMenu(),
            'module_id' => $submenu->getModule(),
            'controller_id' => $submenu->getController(),
            'action_id' => $submenu->getAction(),
        );
        
        if ($submenu->getId()<=0) {
            unset($data['submenu_id']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('submenu_id = ?' => $submenu->getId()));
        }
    }

    public function find($id, Admin_Model_Submenu $submenu) {
        $result = $this->getDbTable()->find($id['id']);

        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $submenu->setId($row->submenu_id)
                   ->setName($row->submenu_name)
                   ->setStatus($row->submenu_status)
                   ->setMenu($row->menu_id)
                   ->setModule($row->module_id)
                   ->setController($row->controller_id)
                   ->setAction($row->action_id);
        return $submenu;
    }

    public function fetchAll() {
        $resultSet = $this->getDbTable()->fetchAll();

        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Admin_Model_Submenu();
            $entry->setId($row->submenu_id)
                  ->setName($row->submenu_name)
                  ->setStatus($row->submenu_status)
                  ->setMenu($row->menu_id)
                  ->setModule($row->module_id)
                   ->setController($row->controller_id)
                   ->setAction($row->action_id);
            $entries[] = $entry;
        }
        return $entries;
    }
    //
    public function del(Admin_Model_Submenu $submenu){
        $where = array("submenu_id = ?" =>$submenu->getId());
        $resultSet = $this->getDbTable()->delete($where);
        return $resultSet;
    }

    /**
     * Método responsavel por selecionar todos os registros com join na table acl.module
     * @author henrique
     * @name selecionaAll
     * @version 1.0
     * @since 21/03/2011
     */
    public function selecionaAll(){
        $select = $this->getDbTable()->select()
                ->setIntegrityCheck(false)
                ->from(array("sb" => "menu.submenu"))
                ->join(array("mn" => "menu.menu"),"mn.menu_id = sb.menu_id",array("mn.menu_name"))
                ->join(array("md" => "acl.acl_module"),"md.module_id = sb.module_id",array("md.module_name"))
                ->join(array("cl" => "acl.acl_controller"),"cl.controller_id = sb.controller_id",array("cl.controller_name"))
                ->join(array("at" => "acl.acl_action"),"at.action_id = sb.action_id",array("at.action_name"));

//        echo "<pre> Dados: ";
//                    print($select);
//                echo "</pre>";
//                die();

        $resultSet = $this->getDbTable()->fetchAll($select);

        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Admin_Model_Submenu();
            $entry->setId($row->submenu_id)
                  ->setName($row->submenu_name)
                  ->setStatus($row->submenu_status)
                  ->setMenu($row->menu_name)
                  ->setModule($row->module_name)
                  ->setController($row->controller_name)
                  ->setAction($row->action_name);

            $entries[] = $entry;
        }
        return $entries;
    }
}